package de.baumann.browser.present.duobao

import de.baumann.browser.api.OdinObserver
import de.baumann.browser.api.net.vo.duobao.Banner
import de.baumann.browser.api.net.vo.duobao.DuoBao
import de.baumann.browser.api.net.vo.duobao.DuoBaoMsg
import de.baumann.browser.base.BasePresenter
import de.baumann.browser.iview.duobao.IDuoBaoHomeView
import de.baumann.browser.model.getDuoBaoList
import de.baumann.browser.model.getDuoBaoMsg
import de.baumann.browser.model.selectHomeCarousel

class DuoBaoHomePresenter : BasePresenter<IDuoBaoHomeView>() {

    fun getBanner() {
        selectHomeCarousel()
                .subscribe(object : OdinObserver<List<Banner>>(view) {
                    override fun onSuccess(data: List<Banner>?) {
                        if (data != null) {
                            view?.setBanners(data)
                        }
                    }
                })
    }

    fun getNewMsg() {
        getDuoBaoMsg()
                .subscribe(object : OdinObserver<DuoBaoMsg>(view) {
                    override fun onSuccess(data: DuoBaoMsg?) {
                        if (data == null) {
                            return
                        }
                        var address = data.address
                        if (address.isEmpty()) {
                            return
                        }

                        address = address.substring(address.length - 9, address.length - 1)

                        view?.setDuoBaoMsg(address)
                    }
                })
    }

    fun getList() {
        getDuoBaoList()
                .subscribe(object : OdinObserver<List<DuoBao>>(view) {
                    override fun onSuccess(data: List<DuoBao>?) {
                        view?.setDuoBaoList(data)
                    }
                })
    }
}